package com.laozhao.test;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import javax.swing.table.*;

public class testFrame extends JFrame {
	int rows = 80;
	int cols = 5;
	private Object[] rowData = new Object[cols];
	private DefaultTableModel model = new DefaultTableModel();
	JScrollPane jp = new JScrollPane();
	private JTable table = new JTable(model);

	public testFrame() {
		for (int i = 0; i < cols; i++)
			model.addColumn("Column" + i);
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++)
				rowData[j] = "(" + i + "," + j + ")";
			model.addRow(rowData);
		}

		Container contentPane = getContentPane();
		jp.getViewport().add(table);

		contentPane.add(new ControlPanel(), BorderLayout.NORTH);
		contentPane.add(jp, BorderLayout.CENTER);

	}

	public static void main(String args[]) {
		testFrame bt = new testFrame();
		bt.setTitle("test   of   AbstractTableModel");
		bt.setSize(400, 400);
		bt.setVisible(true);
	}

	class ControlPanel extends JPanel {

		private JButton scrollButton = new JButton("add&scroll");

		public ControlPanel() {

			scrollButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					String sdata[] = new String[5];
					for (int i = 0; i < 5; i++) {
						sdata[i] = "cell" + i;
					}
					model.addRow(sdata);
					int row = model.getRowCount();
					Rectangle cellrec = table.getCellRect(row - 1, 0, true);
					jp.getViewport().scrollRectToVisible(cellrec);
				}
			});
		}
	}
}